package com.she.iiicoursecenter.activity.tab1_infomation;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast;

import com.she.iiicoursecenter.R;
import com.she.iiicoursecenter.activity.template.TemplateActivity;
import com.she.iiicoursecenter.event.base.Event;
import com.she.iiicoursecenter.event.connect.bean.GetContentBean;
import com.she.iiicoursecenter.event.connect.event.GetContentEvent;
import com.she.iiicoursecenter.event.iface.IEventCallback;
import com.she.iiicoursecenter.eventmanager.KenEventManager;
import com.she.iiicoursecenter.view.tab1.Tab1_CourseSetView;
import com.she.iiicoursecenter.view.tab1.Tab1_CourseSingleView;
import com.she.iiicoursecenter.view.tab1.Tab1_MainView;

public class Tab1_CourseSingle extends TemplateActivity {
	//
	//
	private Tab1_CourseSingleView	mCourseSingleView;
	private ProgressDialog dialog ;
	public Tab1_CourseSingle() {
		// TODO Auto-generated constructor stub
	}
	
	private Handler notifyHandler = new Handler(){
		@Override
		public void dispatchMessage(Message msg) {
			// TODO Auto-generated method stub
			if (msg.obj == null) {
				mCourseSingleView.notifyAdapter();
				dialog.cancel();
			}else{
				mCourseSingleView.notifyAdapter();
				Toast.makeText(Tab1_CourseSingle.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
				dialog.cancel();
			}

			super.dispatchMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mCourseSingleView = new Tab1_CourseSingleView(this);
		
		dialog = new ProgressDialog(this);
		 dialog.setMessage("讀取中...");
		 dialog.setIndeterminate(true);
		 dialog.setCancelable(true);
		 dialog.show();
		 
		// get bundle from previous...
		Bundle bundle = this.getIntent().getExtras();

		// test only!!!
		String contentName = bundle.getString("contentName");
		int contentId = bundle.getInt("contentId");

		//set title
		TextView title = (TextView) mCourseSingleView.findViewById(R.id.tv_title);
		title.setText(contentName);
		if (contentName != null) {
			if (contentName.length()>=8) {
				title.setTextSize(18);
			}
			if (contentName.length()>=10) {
				title.setTextSize(18);
				title.setText(contentName.substring(0, 10)+"...");
			}
		}
		
		//
		setView(mCourseSingleView);
		setContentView(mCourseSingleView);		
		//判斷是否從下一層回來
//		if (Tab1_CourseSingle.this.getApp().getContentSingleBeanData().getResult() != 0 || getApp().tab3InCourseSingle == true || getApp().tab2InCourseSingle == true) {
			GetContentEvent getContentEvent = new GetContentEvent(this, contentId);
			getContentEvent.addCallback(new IEventCallback() {
				
				public void result(Event event, Object result) {
					if (((GetContentBean) result).getResult() == 0) {
						Tab1_CourseSingle.this.getApp().setContentSingleBeanData((GetContentBean) result);
						notifyHandler.sendEmptyMessage(0);
					}else{
						Tab1_CourseSingle.this.getApp().setContentSingleBeanData(new GetContentBean());
						Message msg = notifyHandler.obtainMessage(0, ((GetContentBean) result).getMessage());
						notifyHandler.sendMessage(msg);
					}

				}
				public void progress(Event event, int progress, String tmpPath) {
				}
			});
			KenEventManager.getInstance().execute(getContentEvent);
//		}else{
//			notifyHandler.sendEmptyMessage(0);
//			title.setText(getApp().getContentSingleBeanData().getName());
//		}
//		getApp().tab3InCourseSingle = false;
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onAttachedToWindow() {
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
	}

	@Override
	public void onDetachedFromWindow() {
		// TODO Auto-generated method stub
		super.onDetachedFromWindow();
	}
	
}
